{extend name="public:base"}
{block name='css'}
{/block}

{block name='body'}
<div class="panel panel-right panel-cover">
    <p class="close-panel"><a class="panel-close" href="javascript:;"><i class="iconfont icon-guanbi"></i></a></p>

    <div class="class-panel-m">
        {foreach name="list" item="li" key='key'}
        <div class="t">{$li.title}</div>
        <div class="m">
            {notempty name="li.list"}
            {foreach name="li.list" item="ll"}
            <a href="javascript:;" onclick="selectCateGoods(this, {$ll.id});" {if condition="input('cate_id') eq $ll.id"}class="on js-cate"{else/}class="js-cate"{/if}>{$ll.title}</a>
            {/foreach}
            {/notempty}
        </div>
        {/foreach}
    </div>
</div>

<div class="view view-main white-navbar white-page white-toolbar">
    <div data-name="home" class="page">

        <div class="navbar">
            <div class="navbar-inner">
                <div class="left"><a href="{$my_prev_url|default='/'}" class="external"><i class="iconfont icon-fanhui"></i></a></div>
                <div class="center"><span>添加订单({:input('name')})</span></div>
                <div class="right">
                    <a class="top-right-class panel-open" data-panel="right"><i class="iconfont icon-icon-"></i></a>
                </div>
            </div>
        </div>

        <div class="page-content infinite-scroll-content">

            <div class="me-order goods-t-sta">
                <div class="m" style="min-width:100%;">
                    <a class="on external js-order" data-order="0">综合</a>
                    <a class="external js-order" data-order="1">销量</a>
                    <a class="external js-order" data-order="2">新品</a>
                    <a class="external js-order" data-order="3" data-desc="1">价格</a>
                </div>
            </div>
            <div class="serach">
                <form name="form" action="" id="form" method="post" onsubmit="searchGoods();return false;" >
                    <div class="m">
                        <div class="ico"></div>
                        <div class="key"><input type="text" name="keywords" placeholder="请输入关键字搜索" /></div>
                    </div>
                    <div class="sub"><input type="submit" value="搜索" /></div>
                </form>
            </div>

            <div class="bakcground-10"></div>

            <div class="home-goods-ul">

            </div>
            {php}
            $user_cart = session('user_cart');
            $user_id = input('user_id', 0, 'intval');
            if (isset($user_cart[$user_id])) {
                $num = count($user_cart[$user_id]);
            } else {
                $num = 0;
            }
            {/php}
            <a href="{:url('user_cart')}" class="car-count external"><i class="iconfont icon-gouwuche"></i><span class="js-cart-num" style="display:{gt name='num' value='0'}block{else/}none{/gt};">{$num}</span></a>

            {include file='public:loading' /}

        </div>

    </div>
</div>
{/block}

{block name="js"}
<script type="text/javascript">
    $(function(){
        $('.class-l ul li').click(function(){
            $('.class-l ul li').removeClass('on');
            $('.js-sons').hide();
            $(this).addClass('on');
            $('.js-sons-'+$(this).attr('classid')).show();
        });
        $('.class-l ul li').eq(0).click();
        //排序
        $('.js-order').click(function(){
            $('.home-goods-ul').html('');
            $('.no-more').hide();
            $('.no-data').hide();
            $('.js-order').removeClass('on');
            $(this).addClass('on');
            get_goods_list($('.home-goods-ul').attr('data-cate_id'), $(this).attr('data-order'));
        });

        get_goods_list($('.home-goods-ul').attr('data-cate_id'));
    });
    //触发无限滚动
    $$('.infinite-scroll-content').on('infinite', function () {
        get_goods_list($('.home-goods-ul').attr('data-cate_id'), $('.js-order.on').attr('data-order'));
    });

    //分类
    function selectCateGoods(self, cate_id) {
        $('.js-cate').removeClass('on');
        $(self).addClass('on');
        $('.home-goods-ul').attr('data-cate_id', cate_id);
        $('.icon-guanbi').click();
        $('.js-order').eq(0).click();
    }

    function get_goods_list(cate_id, order, keywords) {
        var total = $('.home-goods-ul .li').length;
        if (total % 10 != 0) {
            return false;
        }

        if ($('.loading').is(':visible') || $('.no-more').is(':visible') || $('.no-data').is(':visible')) {
            return false;
        }
        $('.loading').show();

        var p = total / 10;
        $.post("{:url('index/getGoodsList')}", {page:p+1,user_id:'{:input("user_id")}',name:'{:input("name")}',cate_id:cate_id,order:order,keywords:keywords,handle:3}, function(data){
            var str = '';
            $('.loading').hide();
            if (data.data.data.length > 0) {
                $.each(data.data.data, function(i,n){
                    str += '<div class="li">\n' +
                        '    <div class="m">\n' +
                        '        <a href="{:url("index/goods")}?id='+n.id+'&ha=1"><img onclick="self.location = $(this).parent().attr(\'href\');" src="'+n.img+'" class="lazy lazy-fade-in" ></a>\n' +
                        '        <p class="p1">'+n.title+'</p>\n' +
                        '        <p class="p2">￥'+n.unit_price+'<span class="add-cart" onclick="addCart('+n.id+');"><i class="iconfont icon-icozhuanhuan"></i></span></p>\n' +
                        '    </div>\n' +
                        '</div>';
                });
                if (data.data.data.length < 10) {
                    $('.no-more').show();
                }
                $(str).appendTo($('.home-goods-ul'));
            } else {
                if (total > 0) {
                    $('.no-more').show();
                } else {
                    $('.no-data').show();
                }
            }
        }, 'json');
    }

    function addCart(goods_id) {
        $.post("{:url('addCart')}", {goods_id:goods_id}, function(data){
            messageToast(data.info);
            $('.js-cart-num').text(parseInt($('.js-cart-num').text()) + parseInt(data.data)).show();
        }, 'json');
    }

    function searchGoods() {
        $('.home-goods-ul').html('');
        $('.no-more').hide();
        $('.no-data').hide();
        get_goods_list($('.home-goods-ul').attr('data-cate_id'), $('.js-order.on').attr('data-order'), $('input[name=keywords]').val());
    }
</script>
{/block}